package com.lxmes.gs.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.TaskStatisticsMapper;
import com.lxmes.gs.domain.TaskStatistics;
import com.lxmes.gs.service.ITaskStatisticsService;

/**
 * 任务统计Service业务层处理
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class TaskStatisticsServiceImpl extends ServiceImpl<TaskStatisticsMapper,TaskStatistics> implements ITaskStatisticsService{
    
    @Autowired
    private TaskStatisticsMapper taskStatisticsMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询任务统计
     * 
     * @param id 任务统计主键
     * @return 任务统计
     */
    @Override
    public TaskStatistics selectTaskStatisticsById(Long id){
        return taskStatisticsMapper.selectById(id);
    }

    /**
     * 查询任务统计列表
     * 
     * @param taskStatistics 任务统计
     * @return 任务统计
     */
    @Override
    public List<TaskStatistics> selectTaskStatisticsList(TaskStatistics taskStatistics){
        return taskStatisticsMapper.selectList(Wrappers.lambdaQuery(taskStatistics));
    }

    /**
     * 新增任务统计
     * 
     * @param taskStatistics 任务统计
     * @return 结果
     */
    @Override
    public int insertTaskStatistics(TaskStatistics taskStatistics){
        insertOrUpdatePre(taskStatistics);
        return taskStatisticsMapper.insert(taskStatistics);
    }

    private void insertOrUpdatePre(TaskStatistics taskStatistics) {
        Long currentWorkingHours = ObjectUtil.defaultIfNull(taskStatistics.getCurrentWorkingHours(),0L);
        Double workingHoursUnitPrice = ObjectUtil.defaultIfNull(taskStatistics.getWorkingHoursUnitPrice(), 0D);
        Long accumulatedWorkingHours = ObjectUtil.defaultIfNull(taskStatistics.getAccumulatedWorkingHours(),0L);
        
        taskStatistics.setCurrentCost(currentWorkingHours * workingHoursUnitPrice);
        taskStatistics.setTotalCost(accumulatedWorkingHours * workingHoursUnitPrice);
    }

    /**
     * 修改任务统计
     * 
     * @param taskStatistics 任务统计
     * @return 结果
     */
    @Override
    public int updateTaskStatistics(TaskStatistics taskStatistics){
        insertOrUpdatePre(taskStatistics);
        return taskStatisticsMapper.updateById(taskStatistics);
    }

    /**
     * 批量删除任务统计
     * 
     * @param ids 需要删除的任务统计主键
     * @return 结果
     */
    @Override
    public int deleteTaskStatisticsByIds(Long[] ids){
        return taskStatisticsMapper.deleteTaskStatisticsByIds(ids);
    }

    /**
     * 删除任务统计信息
     * 
     * @param id 任务统计主键
     * @return 结果
     */
    @Override
    public int deleteTaskStatisticsById(Long id){
        return taskStatisticsMapper.deleteById(id);
    }
    @Override
    public String imporIteminfo(List<TaskStatistics> taskStatisticsList, boolean updateSupport) {
        if (StringUtils.isNull(taskStatisticsList) || taskStatisticsList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TaskStatistics taskStatistics : taskStatisticsList)
        {
            try
            {
                BeanValidators.validateWithException(validator, taskStatistics);
                this.insertTaskStatistics(taskStatistics);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
